package com.cn.jinl.service;


import com.cn.jinl.api.IProcureCloseService;
import com.cn.jinl.dao.ProcureCloseMapper;
import com.cn.jinl.dao.ProcureMapper;
import com.cn.jinl.domain.Procure;
import com.cn.jinl.domain.ProcureClose;
import com.cn.jinl.domain.ProcureCloseExample;
import com.cn.jinl.utils.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProcureCloseServiceImpl implements IProcureCloseService {

    @Autowired
    private ProcureCloseMapper procureCloseMapper;

    @Autowired
    private ProcureMapper procureMapper;

    @Override
    public List<ProcureClose> getProcureCloseList(String procureId) throws Exception {
        ProcureCloseExample example = new ProcureCloseExample();
        example.createCriteria().andProcureIdEqualTo(procureId);
        return procureCloseMapper.selectByExample(example);
    }

    @Override
    public String addProcureClose(ProcureClose procureClose) throws Exception {
        String procureId = procureClose.getProcureId();
        if(StringUtils.isEmpty(procureId)){
            return "参数错误！";
        }
        Procure procure = procureMapper.selectByPrimaryKey(procureId);
        if(null == procure){
            return "采购不存在！";
        }
        if(StringUtils.equals(procure.getPayStatus(),"1")){
            return "采购已经支付完成！";
        }
        List<ProcureClose> ProcureCloseList = this.getProcureCloseList(procureId);
        double totalMoney = 0.0;
        for(ProcureClose exist:ProcureCloseList){
            String money = exist.getClosePayMoney();
            totalMoney+= Double.parseDouble(money);
        }
        totalMoney = totalMoney + Double.parseDouble(procureClose.getClosePayMoney());
        if(totalMoney > Double.parseDouble(procure.getTotalMoney())){
            return "支付总计金额大于采购金额！";
        }
        int count = procureCloseMapper.insertSelective(procureClose);
        return CommonUtil.outStr(count);
    }
}
